﻿using System;
using System.Collections.Generic;
using System.Data.Entity.Migrations;
using System.Linq;
using LunchLearn.WebApi.Domain;
using WebGrease.Css.Extensions;

namespace LunchLearn.WebApi.Repository
{
    public class Configuration : DbMigrationsConfiguration<LunchLearnContext>
    {
        public Configuration()
        {
            AutomaticMigrationsEnabled = true;
            AutomaticMigrationDataLossAllowed = true;

        }

        protected override void Seed(LunchLearnContext context)
        {
            context.OrderLines.ForEach(x => context.OrderLines.Remove(x));
            context.Orders.ForEach(x => context.Orders.Remove(x));
            context.Customers.ForEach(x => context.Customers.Remove(x));
            context.Items.ForEach(x => context.Items.Remove(x));
            context.SaveChanges();
            SeedDatabase(context);
        }

        public static void SeedDatabase(LunchLearnContext context)
        {
            var iphone = CreateItem(1, "iPhone", 500, "Images/iphone.jpg");
            var wp = CreateItem(2, "Windows Phone 8", 400, "Images/wp8.jpg");
            var bb = CreateItem(3, "Blackberry", 500, "Images/blackberry.jpg");
            var nk = CreateItem(4, "Nokia 3330", 50, "Images/nokia.jpg");
            var g = CreateItem(5, "Nexus 4", 300, "Images/nexus.jpg");
            var items = new[] { iphone, wp, bb, nk, g };

            var c4 = CreateCustomer(4, "Adrian", "Ortuzar", "http://puck.eu/images/team/pic_adrian.jpg");
            var c5 = CreateCustomer(5, "Albert", "Margarit", "http://puck.eu/images/team/pic_albert.jpg");
            var c6 = CreateCustomer(6, "Andreas", "Prenninger", "http://puck.eu/images/team/pic_andreas.jpg");
            var c7 = CreateCustomer(7, "Andres", "Diaz", "http://puck.eu/images/team/pic_andres.jpg");
            var c8 = CreateCustomer(8, "Anna-Karin", "Vesterberg", "http://puck.eu/images/team/pic_ak.jpg");
            var c9 = CreateCustomer(9, "Carlos", "Armas", "http://puck.eu/images/team/pic_carlos.jpg");
            var c10 = CreateCustomer(10, "Charlotte", "van de Ven", "http://puck.eu/images/team/pic_charlotte.jpg");
            var c11 = CreateCustomer(11, "Krzysztof", "Slupski", "http://puck.eu/images/team/pic_chris.jpg");
            var c12 = CreateCustomer(12, "Edoardo", "Palmieri", "http://puck.eu/images/team/pic_edo.jpg");
            var c13 = CreateCustomer(13, "Eduardo", "Caselles", "http://puck.eu/images/team/pic_edu.jpg");
            var c14 = CreateCustomer(14, "Enric", "Puigventos", "http://puck.eu/images/team/pic_enric.jpg");
            var c15 = CreateCustomer(15, "Isabel", "Losada", "http://puck.eu/images/team/pic_isabel.jpg");
            var c16 = CreateCustomer(16, "Iwan", "Drogt", "http://puck.eu/images/team/pic_iwan.jpg");
            var c17 = CreateCustomer(17, "Jessie", "", "http://puck.eu/images/team/pic_jessie.jpg");
            var c18 = CreateCustomer(18, "Jordi", "Cano", "http://puck.eu/images/team/pic_jordi.jpg");
            var c19 = CreateCustomer(19, "Josh", "Barley", "http://puck.eu/images/team/pic_josh.jpg");
            var c20 = CreateCustomer(20, "Juan", "Trujillo", "http://puck.eu/images/team/pic_juan.jpg");
            var c21 = CreateCustomer(21, "Luis", "Estepa", "http://puck.eu/images/team/pic_luis.jpg");
            var c22 = CreateCustomer(22, "Marc", "Marcet", "http://puck.eu/images/team/pic_marc.jpg");
            var c23 = CreateCustomer(23, "Marc", "Vivas", "http://puck.eu/images/team/pic_marcv.jpg");
            var c24 = CreateCustomer(24, "Maria", "Vidal", "http://puck.eu/images/team/pic_maria.jpg");
            var c25 = CreateCustomer(25, "Marc-Anthony", "Padula", "http://puck.eu/images/team/pic_mpad.jpg");
            var c26 = CreateCustomer(26, "Neus", "Romero", "http://puck.eu/images/team/pic_neus.jpg");
            var c27 = CreateCustomer(27, "Pablo", "Moles", "http://puck.eu/images/team/pic_pablo.jpg");
            var c28 = CreateCustomer(28, "Patrick", "Puck", "http://puck.eu/images/team/pic_patrick.jpg");
            var c29 = CreateCustomer(29, "Pedro", "Maximo", "http://puck.eu/images/team/pic_pedro.jpg");
            var c30 = CreateCustomer(30, "Pere", "Marti", "http://puck.eu/images/team/pic_pere.jpg");
            var c31 = CreateCustomer(31, "Raimon", "Lapuente", "http://puck.eu/images/team/pic_raimon.jpg");
            var c32 = CreateCustomer(32, "Ricard", "Cabrera", "http://puck.eu/images/team/pic_ricard.jpg");
            var c33 = CreateCustomer(33, "Sergi", "Coll", "http://puck.eu/images/team/pic_sergi.jpg");

            var customers = new[] { c4,c5,c6,c7,c8,c9,c10,c11,c12,c13,c14,c15,c16,c17,c18,c19,c20,c21,c22,c23,c24,c25,c26,c27,c28,c29,c30,c31,c32,c33 };

            var orders = new List<Order> {
                CreateOrder(1,new []{ iphone, wp }, c11, true),
                CreateOrder(2,new []{  wp }, c21,true),
                CreateOrder(3,new []{ nk, g }, c15,false),
                CreateOrder(4,new []{ iphone, bb }, c17,true),
                CreateOrder(5,new []{ nk,bb }, c29,false),
                CreateOrder(6,new []{ g }, c4,true)
           };

            Array.ForEach(customers, c => context.Customers.Add(c));
            Array.ForEach(items, i => context.Items.Add(i));
            orders.ForEach(o => context.Orders.Add(o));
            context.SaveChanges();
        }

        private static Customer CreateCustomer(int id, string name, string surname, string imageUrl)
        {
            return new Customer
            {
                Id = id,
                Name = name,
                Surname = surname,
                ImageUrl = imageUrl
            };
        }

        private static Item CreateItem(int id, string name, int price, string imageUrl)
        {
            return new Item
            {
                Id = id,
                ItemCode = name,
                Price = price,
                ImageUrl = imageUrl
            };
        }

        private static Order CreateOrder(int id, IEnumerable<Item> items, Customer customer, bool isPending)
        {
            var rnd = new Random();
            var orderLines = CreateOrderLines(id, items);
            var order = new Order
            {
                Id = id,
                CreationDate = DateTime.Now,
                ReferenceId = Guid.NewGuid(),
                CustomerId = customer.Id,
                OrderLines = orderLines,
                IsPending = isPending,
                IsActive =  true
            };

            order.TotalPaid = (order.TotalAmount / 2) + rnd.Next(0, 50);
            return order;
        }

        private static IList<OrderLine> CreateOrderLines(int orderId, IEnumerable<Item> items)
        {
            var rnd = new Random();
            return items.Select(item => new OrderLine
            {
                ItemId = item.Id,
                OrderId = orderId,
                Quantity = rnd.Next(1, 3)
            }).ToList();
        }
    }
}